package application;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileScan {

    /**
     * @param filepath 文件路径
     * @return 返回 ip:port 的List&lt;Map&gt;
     */
    public List<Bean> fileScan(String filepath) {

        File file = new File(filepath);
        List<Bean> list = new ArrayList<>();

        if (filepath.isEmpty()) {
            return list;
        }

        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));

            String str;

            while ((str = bufferedReader.readLine()) != null) {

                if (!str.isEmpty() && !str.startsWith("#")) {
                    String[] arr = str.split(":");
                    Bean bean = null;

                    if (arr.length > 1) {
                        bean = new Bean(arr[0], arr[1]);

                    } else if (arr.length > 0) {
                        bean = new Bean(arr[0]);
                    }

                    list.add(bean);
                }
            }

            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }
}
